package cn.iocoder.yudao.module.orderview.controller.admin;

import cn.hutool.core.lang.UUID;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.orderview.controller.admin.vo.orderview.TriggerOrderViewTaskReqVO;
import cn.iocoder.yudao.module.orderview.controller.admin.vo.orderview.OrderViewRespVO;
import cn.iocoder.yudao.module.orderview.convert.orderview.OrderViewConvert;
import cn.iocoder.yudao.module.orderview.dal.redis.orderview.OrderViewRedisDAO;
import cn.iocoder.yudao.module.orderview.dal.redis.orderview.snapshot.OrderViewAggregationSnapshot;
import cn.iocoder.yudao.module.orderview.service.orderview.OrderViewService;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.module.orderview.dal.redis.RedisKeyConstants.ORDER_VIEW_SNAPSHOT_PREFIX;
import static cn.iocoder.yudao.module.orderview.dal.redis.RedisKeyConstants.ORDER_VIEW_TASK_PREFIX;

@Tag(name = "管理后台 - 订单视图")
@RestController
@RequestMapping("/orderview")
@Validated
@Slf4j
public class OrderViewController {

    @Resource
    OrderViewService orderViewService;
    @Resource
    RedisTemplate redisTemplate;

    @Resource
    OrderViewRedisDAO  orderViewRedisDAO;

    @GetMapping("/test")
    @Operation(summary = "获取 test 信息")
    public CommonResult<String> get() {
        return success("true");
    }


    @GetMapping("/get-orderview-by-product-id-on-sync")
    @Operation(summary = "同步根据商品id获取订单视图信息")
    @Parameter(name = "productId", description = "商品id", required = true, example = "35942")
    public CommonResult<OrderViewRespVO> getOrderViewByProductIDOnSync(@RequestParam("productId") Long productId) {
        OrderViewRespVO orderViewRespVO = orderViewService.getOrderViewByProductIDOnSync(productId);
        return success(orderViewRespVO);
    }


    @PostMapping("/trigger-orderview-on-asyn-task")
    @Operation(summary = "创建异步查询任务")
    public CommonResult<String> triggerOrderViewOnAsynTask(@Valid @RequestBody TriggerOrderViewTaskReqVO triggerOrderviewTaskReqVO) {
        return success(orderViewService.triggerOrderViewOnAsynTask(triggerOrderviewTaskReqVO.getProductId()));
    }

    @GetMapping("/get-orderview-by-request-id-on-asyn")
    @Operation(summary = "异步根据查询任务请求id获取订单视图信息-前端每两秒轮询一次查询结果")
    @Parameter(name = "requestId", description = "请求id", required = true, example = "1024")
    public CommonResult<OrderViewRespVO> getOrderViewByRequestIDOnAsyn(@RequestParam("requestId") String requestId) {
        String statusRedisKey = formatKey(requestId);
        String status = (String) redisTemplate.opsForValue().get(statusRedisKey);
        if (!"completed".equals(status)) {
            return error(202, "数据尚未准备好，请稍后重试");

        }
        // 聚合数据已完成，返回完整视图
        OrderViewAggregationSnapshot orderView = orderViewRedisDAO.get(requestId);
        log.info("[OrderViewController][订单视图redis快照 orderView({})]", orderView);
        return success(OrderViewConvert.INSTANCE.convert(orderView));
    }

    private static String formatKey(String requestId) {
        return String.format(ORDER_VIEW_TASK_PREFIX, requestId);
    }

    private static String formatKey1(String requestId) {
        return String.format(ORDER_VIEW_SNAPSHOT_PREFIX, requestId);
    }



}
